//
//  MessageBubble.m
//  MiniWeChat
//
//  Created by ByteDance on 2025/6/27.
//

#import "MessageBubble.h"
#import "TextWithBubble.h"
#import "Masonry.h"
#import "DBStorageService.h"

@interface MessageBubble ()

@property(nonatomic, strong) UIImageView *avatarImageView; // 头像
@property(nonatomic, strong) UILabel *nicknameLabel; // 昵称
@property(nonatomic, strong) TextWithBubble *contentBubble; // 带气泡的消息内容
@property(nonatomic, strong) TextWithBubble *timeBubble; // 时间气泡
//@property(nonatomic, strong) TextWithBubble *referenceBubble; // 引用气泡

@property(nonatomic, assign) CGFloat bubbleWidth;
@property(nonatomic, assign) CGFloat bubbleHeight;

@property(nonatomic, assign) BOOL isMine;
@end


@implementation MessageBubble

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    // 头像
    self.avatarImageView = [[UIImageView alloc] init];
    self.avatarImageView.clipsToBounds = YES;
    self.avatarImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.avatarImageView.layer.cornerRadius = 4; // 圆角
    [self.contentView addSubview:self.avatarImageView];
    
    // 昵称
    self.nicknameLabel = [[UILabel alloc] init];
    self.nicknameLabel.textColor = [UIColor blackColor];
    self.nicknameLabel.font = [UIFont systemFontOfSize:17.0f];
    [self.contentView addSubview:self.nicknameLabel];
    
    // 消息气泡
    self.contentBubble = [[TextWithBubble alloc] init];
    [self.contentBubble setTextColor:[UIColor whiteColor]];
    [self.contentBubble setFont:[UIFont systemFontOfSize:16.0]];
    [self.contentBubble setNumberOfLines:0];
    [self.contentBubble setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.contentBubble.layer.cornerRadius = 5;
    [self.contentBubble setInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [self.contentBubble setBackground:[UIColor grayColor]];
    [self.contentView addSubview:self.contentBubble];
}

- (void)setupConstraints {
}

- (void)setMessage:(MessageModel *)message {
    self.nicknameLabel.text = @"昵称";
    
    self.isMine = [message.sender isEqualToString:@"1001"];
    DBStorageService *storage = [DBStorageService sharedInstance];
    UserModel *sender = [storage fetchUserById:message.sender];
    [self.avatarImageView setImage:[UIImage imageNamed:sender.avatar]];
    CGFloat maxWidth = 220;
    CGSize size = [message.content boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName: _contentBubble.font}
                                                context:nil].size;
    _bubbleWidth = size.width + 15, _bubbleHeight = size.height;
    
    [self.contentBubble setTextStr:message.content];
    self.contentBubble.lineBreakMode = NSLineBreakByTruncatingTail;
    
    [self.avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        if (self.isMine) {
            make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-10);
        } else {
            make.left.mas_equalTo(self.contentView.mas_left).mas_offset(10);
        }
        make.top.mas_equalTo(self.contentView.mas_top).mas_offset(10);
        make.bottom.lessThanOrEqualTo(self.contentView.mas_bottom).mas_offset(-10);
        make.width.height.mas_equalTo(48);
    }];
    
    [self.contentBubble mas_remakeConstraints: ^(MASConstraintMaker *make) {
        make.width.equalTo(@(_bubbleWidth));
        if (self.isMine) {
            make.right.mas_equalTo(self.avatarImageView.mas_left).mas_offset(-10);
        } else {
            make.left.mas_equalTo(self.avatarImageView.mas_right).mas_offset(10);
        }
        make.top.equalTo(self.contentView.mas_top).mas_offset(15);
        make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-10);
    }];
}


- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
